package com.dao;

import com.bean.UpdatePws;
import com.bean.User;
import com.bean.UserPage;
import com.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @ClassName UserDao
 * @Desc TODO
 * @Author xiaoding
 * @Date 2021/1/10 19:46
 * @Version 1.0
 */

public class UserDao {
    /*
     * @Author LoginFlag
     * @Description 判断登录是否成功
     * @Date 10:40 2021/1/11
     * @Param users
     * @return com.bean.User
     */
    public User LoginFlag(User users) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.loginFlag";
        User user = session.selectOne(sql,users);
        return user;
    }

    /*
     * @Author isRepetitive
     * @Description 获取所有用户账号、邮箱、判断内容是否重复
     * @Date 11:41 2021/1/21
     * @Param
     * @return java.util.List<com.bean.User>
     */
    public List<User> isRepetitive() {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.isRepetitive";
        List<User> user = session.selectList(sql);
        return user;
    }

    /*
     * @Author userSelect
     * @Description 查询所有用户
     * @Date 11:00 2021/1/11
     * @Param userPage
     * @return java.util.List<com.bean.User>
     */
    public List<User> userSelect(UserPage userPage) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.userSelect";
        List<User> list = session.selectList(sql,userPage);
        return list;
    }

    /*
     * @Author userSelectId
     * @Description 根据用户Id查询用户
     * @Date 21:41 2021/1/20
     * @Param uId
     * @return com.bean.User
     */
    public User userSelectId(int uId) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.userSelectId";
        User user = session.selectOne(sql,uId);
        return user;
    }

    /*
     * @Author userSelects
     * @Description 无分页查询所有用户
     * @Date 19:35 2021/1/20
     * @Param
     * @return java.util.List<com.bean.User>
     */
    public List<User> userSelects() {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.userSelects";
        List<User> list = session.selectList(sql);
        return list;
    }

    /*
     * @Author userSearch
     * @Description 根据名称查询所有用户
     * @Date 11:11 2021/1/11
     * @Param userPage
     * @return java.util.List<com.bean.User>
     */
    public List<User> userSearch(UserPage userPage) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.userSearch";
        List<User> list = session.selectList(sql,userPage);
        return list;
    }

    /*
     * @Author userCount
     * @Description 查询所有用户数量
     * @Date 11:12 2021/1/11
     * @Param
     * @return int
     */
    public int userCount() {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.userCount";
        int a = session.selectOne(sql);
        return  a;
    }

    /*
     * @Author userSearchCount
     * @Description 根据名称模糊查询用户
     * @Date 11:13 2021/1/11
     * @Param uName
     * @return int
     */
    public int userSearchCount(String uName) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.userSearchCount";
        int a = session.selectOne(sql,"%" + uName + "%");
        return  a;
    }

    /*
     * @Author addUser
     * @Description 添加用户
     * @Date 11:16 2021/1/11
     * @Param user
     * @return int
     */
    public int addUser(User user) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.addUser";
        int a = session.insert(sql,user);
        session.commit();
        return a;
    }

    /*
     * @Author updateUser
     * @Description 修改用户信息
     * @Date 10:41 2021/1/11
     * @Param user
     * @return int
     */
    public int updateUser(User user) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.updateUser";
        int a = session.update(sql,user);
        session.commit();
        return a;
    }

    /*
     * @Author updatePws
     * @Description 修改密码
     * @Date 14:49 2021/1/17
     * @Param updatePws
     * @return int
     */
    public int updatePws(UpdatePws updatePws) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.updatePws";
        int a = session.update(sql,updatePws);
        session.commit();
        return a;
    }

    /*
     * @Author deleteUser
     * @Description 根据用户Id删除，指定用户
     * @Date 11:17 2021/1/11
     * @Param uId
     * @return int
     */
    public int deleteUser(int uId) {
        SqlSession session = MyBatisUtils.getSqlSession();
        String sql = "com.resources.UserMapping.deleteUser";
        int a = session.delete(sql,uId);
        session.commit();
        return a;
    }
}
